<template>
  <view class="template-circle tn-safe-area-inset-bottom">
    <!-- 顶部自定义导航 -->
    <tn-nav-bar 
    :isBack="false" 
    :bottomShadow="false" 
    backgroundColor="#fff">
      <view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
        <!-- 个人圈子，默认显示自己头像，当有消息互动的时候，显示别人的头像，并且有一个红点点 -->
        <view class="tn-circles-tabbar">
          <tn-tabs :list="scrollList" 
          :current="current" @change="tabChange" 
          activeColor="#262626" 
          inactiveColor="#fff"
          :height="88"
          :showBar="false"
          :bold="true" :fontSize="36"></tn-tabs>
        </view>
        <view class="pin-button">
          <view class="tn-pin-btn" @tap.stop="goToPin">
            <text class="xicon-camera"></text>
          </view>
        </view>
      </view>
    </tn-nav-bar>
    
    <!-- 发现 -->
    <view class="" v-if="current==0">
      <scroll-view scroll-y="true"
        :scroll-anchoring="true" 
        :refresher-threshold="100"
        :refresher-enabled="true"
        :enable-back-to-top="true"
        :refresher-triggered="triggered"
         @refresherrefresh="refreshList" 
         @refresherrestore="onRestore" 
        scroll-with-animation
        class="classics-scroll" 
        @scrolltolower="getList()"
      >
        <view 
        class="tn-flex tn-flex-direction-column 
        tn-margin-top-sm tn-margin-bottom"
        :style="{paddingTop: vuex_custom_bar_height + 'px'}" 
        >
          <!-- 图文信息 -->
          <block v-for="(item,index) in list">
            <view class="blogger__item" :key="index">
              <view class="blogger__author tn-flex tn-flex-row-between tn-flex-col-center">
                <view class="justify__author__info" >
                  <view class="tn-flex tn-flex-row-center">
                    <view class="tn-flex tn-flex-row-center tn-flex-col-center">
                      <view class="">
                        <tn-avatar
                          class=""
                          shape="circle"
                          :src="item.avatar || 'https://fastcdn.hidino.com/hiotk/user.png'"
                          size="lg">
                        </tn-avatar>
                      </view>
                      <view class="tn-padding-right tn-text-ellipsis">
                        <view class="tn-padding-right 
                        blogger__nickname
                        tn-padding-left-sm tn-text-bold tn-text-lg">
                          {{ item.nickname ? item.nickname : '匿名' }}
                          
                        </view>
                        <view class="tn-padding-right blogger__time tn-padding-left-sm 
                        tn-padding-top-xs">
                          {{ parseDate(item.createDate) }}
                        </view>
                      </view>
                    </view>
                  </view>
                </view>
                <view class="blogger__author__btn justify-content-item tn-flex-col-center tn-flex-row-center">
                  <!-- 为什么不放关注按钮，因为快餐文化的世界，关注按钮放在外面没必要 -->
                  <text class="tn-icon-more-vertical blogger__more tn-text-bold tn-text-xxl"></text>
                </view>
              </view>
             
              <view class="blogger__desc tn-margin-top-sm tn-margin-bottom-sm tn-text-justify tn-flex-col-center tn-flex-row-left"
               @click="gotToDetails(item.id)">
                <view v-for="(label_item,label_index) in item.labels" 
                :key="label_index" class="blogger__desc__label
                 tn-float-left tn-margin-right 
                 tn-round tn-text-sm tn-text-bold">
                  <text class="blogger__desc__label--prefix">#</text> 
                  <text class="tn-text-df blogger__desc__label_text">{{ label_item }}</text>
                </view>
                <!-- 不用限制长度了，因为发布的时候限制长度了-->
                <text v-if="!item.labels || item.labels.length < 4" class="blogger__desc__content tn-flex-1 tn-text-justify tn-text-df">{{ item.record }}</text>  
              </view>
              
              <block v-if="item.albums">
                <view v-if="[1,2,4].indexOf(item.albums.length) != -1" class="tn-padding-top-xs" 
                >
                  <image v-for="(image_item,image_index) in item.albums" :key="image_index" 
                    class="blogger__main-image"
                    @click="previewImages(item.albums, image_index)"
                    :class="{
                      'blogger__main-image--1 tn-margin-bottom-sm': item.albums.length === 1,
                      'blogger__main-image--2 tn-margin-right-sm tn-margin-bottom-sm': item.albums.length === 2 || item.albums.length === 4
                    }"
                    :src="image_item"
                    mode="aspectFill"
                  ></image>
                </view>
                <view v-else class="tn-padding-top-xs">
                  <tn-grid  hoverClass="none" :col="3">
                    <block v-for="(image_item,image_index) in item.albums" :key="image_index">
                      <tn-grid-item style="width: 30%;margin: 10rpx;">
                        <image
                          @click="previewImages(item.albums, image_index)"
                          class="blogger__main-image blogger__main-image--3"
                          :src="image_item"
                          mode="aspectFill"
                        ></image>
                      </tn-grid-item>
                    </block>
                  </tn-grid>
                </view>
              </block>
             
              <view @click="gotToDetails(item.id)" class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin-top-xs">
                <view class="justify-content-item blogger__count__area tn-text-center">
                  <view class="">
                    <text class="blogger__count-icon xicon-bearfoot"></text> 
                    <text class="tn-padding-right">{{ item.cmtNum }}</text>
                    <text class="blogger__count-icon tn-icon-comment"></text>
                    <text class="tn-padding-right">{{ item.cmtNum }}</text>
                    <text class="blogger__count-icon tn-icon-like"></text>
                    <text class="">{{ item.likeNum }}</text>
                  </view>
                </view>
                <!-- <view class="justify-content-item tn-flex tn-flex-col-center">
                  <view style="margin-right: 10rpx;margin-left: 20rpx;">
                    <tn-avatar-group :lists="item.cmtAvatar || []" size="sm"></tn-avatar-group>
                  </view>
                  <text class="tn-color-gray">{{ item.cmtNum }}人</text>
                </view> -->
              </view>
            </view>
            
            <!-- 边距间隔 -->
            <view class="tn-strip-bottom" v-if="index != list.length - 1"></view>
          </block>
          
          <!-- 边距间隔 -->
          <view class="tn-strip-bottom"></view>
              
        </view>
      </scroll-view>
      <view class='tn-tabbar-height'></view>
      
    </view>
    
    <!-- 视频，采用轮播形式，放10个视频左右，小程序性能决定了这个不能太骚，如果你想骚，可以把微信买下来 -->
    <view class="" v-if="current==1">
      <test-ball></test-ball>
    </view>
    
    <!-- 活动 -->
    <view class="" v-if="current==2">
      <view class="tn-margin-bottom-lg" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
        <block v-for="(item, index) in reserve" :key="index">
          <view class="article-shadow tn-margin" @click="tn('/circlePages/group')">
            <view class="tn-flex">
              <view class="image-pic tn-margin-sm" :style="'background-image:url(' + item.userAvatar + ')'">
                <view class="image-article">
                </view>
              </view>
              <view class="tn-margin-sm tn-padding-top-xs" style="width: 100%;">
                <view class="tn-text-lg tn-text-bold clamp-text-1 tn-text-justify">
                  {{ item.title }}
                </view>
                <view class="tn-flex tn-flex-row-between tn-flex-col-between tn-padding-top-xs" style="min-height: 105rpx;">
                  <!-- <text class="tn-text-df tn-color-gray clamp-text-2 tn-text-justify">
                    {{ item.desc }}
                  </text> -->
                  <view class="justify-content-item tn-flex tn-flex-col-center">
                    <tn-avatar-group :lists="item.viewUser.latestUserAvatar" size="sm"></tn-avatar-group>
                  </view>
                  <view class="justify-content-item tn-flex tn-flex-col-center">
                    <text class="tn-color-gray">{{ item.viewUser.viewUserCount }} 人参与</text>
                  </view>
                </view>
                <view class="tn-flex tn-flex-row-between tn-flex-col-between">
                  <view v-for="(label_item,label_index) in item.label" :key="label_index"
                    class="justify-content-item tn-tag-content__item tn-margin-right tn-round tn-text-sm tn-text-bold" :class="[`tn-bg-${item.color}--light tn-color-${item.color}`]">
                    <text class="tn-tag-content__item--prefix">#</text> {{ label_item }}
                  </view>
                  <view class="justify-content-item tn-color-gray tn-text-center tn-color-gray--disabled" style="padding-top: 5rpx;">
                    <text class="tn-icon-eye tn-padding-right-xs tn-text-lg"></text>
                    <text class="tn-padding-right tn-text-df">{{ item.collectionCount }}</text>
                    <text class="tn-icon-fire tn-padding-right-xs tn-text-lg"></text>
                    <text class="tn-text-df">{{ item.likeCount }}</text>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </block>
      </view>
      <view class='tn-tabbar-height'></view>
    </view>
  </view>
</template>

<script>
  import { getRestaurantRecords } from '@/libs/js/home.js';
  import { date } from '@/libs/js/utils.js';
	import TestBall from '../test-ball/test-ball.vue'
  export default {
    name: 'CirclePage',
    components: {
      TestBall
    },
    data(){
      return {
          cardCur: 0,
          current: 0,
          scrollList: [
            {name: '发现'},
            {name: '星球'},
            // {name: '话题', count: ''}
          ],
          latestUserAvatar: [
            {src: 'https://fastcdn.hidino.com/dinner/i1.png'},
            {src: 'https://fastcdn.hidino.com/dinner/i2.png'},
            {src: 'https://fastcdn.hidino.com/dinner/i3.png'},
            {src: 'https://fastcdn.hidino.com/dinner/i1.png'},
          ],
          bloggerList: [{
            id: 0,
            type: 'image',
            name: '炸鸡',
            text: '629人关注',
            url: 'https://fastcdn.hidino.com/dinner/i1.png',
          }, {
            id: 1,
            type: 'image',
            name: '大排档',
            text: '688人关注',
            url: 'https://fastcdn.hidino.com/dinner/i1.png',
          }, {
            id: 2,
            type: 'image',
            name: '苍蝇馆子',
            text: '552人关注',
            url: 'https://fastcdn.hidino.com/dinner/i1.png',
          }, {
            id: 3,
            type: 'image',
            name: '日料',
            text: '105人关注',
            url: 'https://fastcdn.hidino.com/dinner/i2.png',
          }, {
            id: 4,
            type: 'image',
            name: '烧烤',
            text: '387人关注',
            url: 'https://fastcdn.hidino.com/dinner/i3.png',
          }, {
            id: 5,
            type: 'image',
            name: '火锅',
            text: '643人关注',
            url: 'https://fastcdn.hidino.com/dinner/i1.png',
          }],
          // 内容默认隐藏显示的高度
          contentHideShowHeight: 0,
          adList: [
            {image: 'https://fastcdn.hidino.com/dinner/i3.png'},
            {image: 'https://fastcdn.hidino.com/dinner/i4.png'},
            {image: 'https://fastcdn.hidino.com/dinner/i5.png'},
            {image: 'https://fastcdn.hidino.com/dinner/i6.png'},
            {image: 'https://fastcdn.hidino.com/dinner/i7.png'}
          ],
          adAutoplay: false,
          listQuery: {
            limit: 20,
            page: 1
          },
          triggered: false,
          /* 压屏窗*/
          show2: false,
          loading: false,
          list: [],
          loaded: false,
          maskCloseable: true,
      }
    },
    mounted() {
      const that = this;
      console.log('mounted')
      uni.getLocation({
      	type: 'wgs84',
        timeout: 1,
      	success: function(res) {
      		that.listQuery.lng = res.longitude;
      		that.listQuery.lat = res.latitude;
      		that.getList();
      	},
      	fail: function() {
      		that.getList();
      	}
      })
    },
    onShow() {
      this.adAutoplay = true
    },
    onHide() {
      this.adAutoplay = false
    },
    methods: {
      // tab选项卡切换
      tabChange(index) {
        this.current = index
      },
      parseDate(d) {
        return date('Y年m月d日H时i分s秒', d)
      },
      onRestore() {
        this.triggered = false; 
      },
      goToPin() {
        uni.navigateTo({
          url: '/circlePages/edit'
        })
      },
      gotToDetails(id) {
        uni.navigateTo({
          url: '/circlePages/details?id=' + id
        })
      },
      refreshList() {
        if (this.loading) return;
        this.loaded = false;
        this.triggered = true;
        console.log('refresh')
        this.listQuery.page = 1;
        this.getList();
        setTimeout(() => {
          this.triggered = false;
        }, 3000)
      },
      previewImages(albums, index) {
        uni.previewImage({
          urls: albums,
          current: index
        });
      },
      getList() {
        if(this.loading || this.loaded){
          return;
        }
        this.loading = true;
        getRestaurantRecords(this.listQuery).then(res => {
          this.triggered = false;
          if (res.code === 0 && res.data && res.data.list && res.data.list.length) {
          	const t = res.data.list.map(r => {
          		return {
                ...r,
          			title: r.record,
          			address: r.addresses,
          			albums: r.albums && r.albums.match(/http/gi) ? r.albums.split(',') : '',
          			labels: r.restaurantsLabels ? JSON.parse(r.restaurantsLabels) : [],
          		}
          	})
            if(this.listQuery.page == 1){
              this.list = t;
            }else{
              this.list = this.list.concat(t);
            }
          
            if(t.length < this.listQuery.limit){
              this.loaded = true;
            }
            console.log(this.list);
          }else{
            this.loaded = true;
          }
          this.listQuery.page++;
          this.loading = false;
        });
      },
      // 跳转
      tn(e) {
      	uni.navigateTo({
      		url: e,
      	});
      },
      
     
      // 切换内容的显示与隐藏
      switchContentShowStatus(index) {
        const contentItem = this.content[index]
        contentItem.showAllContent = !contentItem.showAllContent
        this.$set(this.content, index, contentItem)
      }
    }
  }
</script>
<style lang="scss" scoped>
  .template-circle{
    max-height: 100vh;
  }
  .tn-tabbar-height {
  	min-height: 120rpx;
  	height: calc(140rpx + env(safe-area-inset-bottom) / 2);
  }
  /* 自定义导航栏内容 start */
  .custom-nav {
    height: 100%;
    background: linear-gradient(180deg, #FF8961 0%, rgba(255, 137, 97, 0.8) 100%);
    &__back {
      margin: auto 5rpx;
      font-size: 40rpx;
      margin-right: 10rpx;
      margin-left: 30rpx;
      flex-basis: 5%;
    }
    
    &__search {
      flex-basis: 60%;
      width: 100%;
      height: 100%;
      
      &__box {
        width: 100%;
        height: 70%;
        padding: 10rpx 0;
        margin: 0 30rpx;
        border-radius: 60rpx 60rpx 0 60rpx;
        font-size: 24rpx;
      }
      
      &__icon {
        padding-right: 10rpx;
        margin-left: 20rpx;
        font-size: 30rpx;
      }
      
      &__text {
        color: #AAAAAA;
      }
    }
    .pin-button{
      display: flex;
      flex-direction: row;
      align-self: center;
      justify-self: flex-end;
      flex: 1 1 0;
      justify-content: flex-end;
      .tn-pin-btn{
        padding: 0rpx 30rpx;
        font-size: 48rpx;
        color: #fff;
        display: flex;
        align-items: center;
      }
    }
  }
  .logo-image{
    width: 60rpx;
    height: 60rpx;
    position: relative;
    margin-top: -15rpx;
  }
  .logo-pic{
    background-size: cover;
    background-repeat:no-repeat;
    // background-attachment:fixed;
    background-position:top;
    border-radius: 50%;
  }
  /* 自定义导航栏内容 end */
  /* 博主头像 start*/
  .image-circle{
    // padding: 95rpx;
    width: 190rpx;
    height: 190rpx;
    font-size: 40rpx;
    font-weight: 300;
    position: relative;
  }
  .image-pic{
    background-size: cover;
    background-repeat:no-repeat;
    // background-attachment:fixed;
    background-position:top;
    border-radius: 10rpx;
  }
  .classics-scroll{
    height: 100vh;

    position: sticky;
    top: 0;
    box-sizing: border-box;
  }
  
   /* 间隔线 start*/
  .tn-strip-bottom {
   width: 100%;
   border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
  }
   /* 间隔线 end*/
  
  /* 广告内容 start */
  .ad-image{
    width: 80rpx;
    height: 80rpx;
    position: relative;
  }
  .ad-pic{
    background-size: cover;
    background-repeat:no-repeat;
    // background-attachment:fixed;
    background-position:top;
    border-radius: 20%;
  }
  /* 自定义导航栏内容 end */
  
  
  /* 全屏轮播  start*/
  .card-swiper {
    height: 100vh !important;
  }
    
  .card-swiper swiper-item {
    width: 750rpx !important;
    left: 0rpx;
    box-sizing: border-box;
    overflow: initial;
  }
    
  .card-swiper swiper-item .swiper-item {
    width: 100%;
    display: block;
    height: 100vh;
    border-radius: 0rpx;
    transform: scale(1);
    transition: all 0.2s ease-in 0s;
    overflow: hidden;
  }
    
  .card-swiper swiper-item.cur .swiper-item {
    transform: none;
    transition: all 0.2s ease-in 0s;
  }
    
  .card-swiper swiper-item .swiper-item-png {
    margin-top: -50vh;
    width: 100%;
    display: block;
    border-radius: 0rpx;
    transform: translate(1040rpx, 20rpx) scale(0.5, 0.5);
    transition: all 0.6s ease 0s;
    // overflow: hidden;
  }
    
  .card-swiper swiper-item.cur .swiper-item-png {
    margin-top: -100vh;
    width: 900rpx;
    transform: translate(-80rpx, 0rpx) scale(1, 1);
    transition: all 0.6s ease 0s;
  }
  .image-banner{
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .image-banner image{
    width: 100%;
  }
  
  /* 轮播指示点 start*/
  .indication{
    z-index: 9999;
    width: 100%;
    height: 36rpx;
    position: fixed;
    // display:flex;
    display: block;
    flex-direction:row;
    align-items:center;
    justify-content:center;
  }
  
  .spot{
    background-color: #000;
    opacity: 0.3;
    width: 10rpx;
    height: 10rpx;
    border-radius: 20rpx;
    margin: 20rpx 0 !important;
    left: 95vw;
    top: -60vh;
    position: relative;
  }
  
  .spot.active{
    opacity: 0.6;
    height: 30rpx;
    background-color: #000;
  }  
  
  /* 资讯主图 start*/
  .image-article {
    border-radius: 8rpx;
    border: 1rpx solid #F8F7F8;
    width: 200rpx;
    height: 200rpx;
    position: relative;
  }
  
  .image-pic {
    background-size: cover;
    background-repeat: no-repeat;
    // background-attachment:fixed;
    background-position: top;
    border-radius: 10rpx;
  }
  
  .article-shadow {
    border-radius: 15rpx;
    box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
  }
  
  /* 文字截取*/
  .clamp-text-1 {
    -webkit-line-clamp: 1;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  
  .clamp-text-2 {
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  
  /* 标签内容 start*/
  .tn-tag-content {
    &__item {
      display: inline-block;
      line-height: 35rpx;
      padding: 5rpx 25rpx;
  
      &--prefix {
        padding-right: 10rpx;
      }
    }
  }
  
  
  /* 图标容器9 start */
  .icon9 {
    &__item {
      width: 30%;
      background-color: #FFFFFF;
      border-radius: 10rpx;
      padding: 30rpx;
      margin: 20rpx 10rpx;
      transform: scale(1);
      transition: transform 0.3s linear;
      transform-origin: center center;
      
      &--icon {
        width: 110rpx;
        height: 110rpx;
        font-size: 65rpx;
        border-radius: 50%;
        margin: 20rpx 40rpx;
        position: relative;
        z-index: 1;
        
        &::after {
          content: " ";
          position: absolute;
          z-index: -1;
          width: 100%;
          height: 100%;
          left: 0;
          bottom: 0;
          border-radius: inherit;
          opacity: 1;
          transform: scale(1, 1);
          background-size: 100% 100%;
          background-image: url(https://tnuiimage.tnkjapp.com/cool_bg_image/icon_bg5.png);
        }
      }
    }
  }
  
  
  /* 悬浮 */
  .tnxuanfu{
    animation: suspension 3s ease-in-out infinite;
  }
  
  @keyframes suspension {
    0%, 100% {
      transform: translateY(0);
    }
    50% {
      transform: translateY(-0.8rem);
    }
  }
  /* 悬浮按钮 */
  .button-shop {
    width: 90rpx;
    height: 90rpx;
    display: flex;
    flex-direction: row;
    position: fixed;
    /* bottom:200rpx;
      right: 20rpx; */
    left: 5rpx;
    top: 5rpx;
    z-index: 1001;
    border-radius: 100px;
    opacity: 0.9;
  }
  
  
  /* 按钮 */
  .edit {
    bottom: 300rpx;
    right: 75rpx;
    position: fixed;
    z-index: 9999;
  }
  
  
  .pa,
  .pa0 {
    position: absolute
  }
  
  .pa0 {
    left: 0;
    top: 0
  }
  
  
  .bg0 {
    width: 100rpx;
    height: 100rpx;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  
  .bg1 {
    width: 100%;
    height: 100%;
  }
  
 
</style>
